/**
 * 二分法
 * @param {*} x 
 * @returns 
 */
function sqrt(x) {
    // write code here
    if (x <= 0) return 0
    let left = 1, right = x
    for (; ;) {
        let middle = (left + right) >> 1
        if (middle <= x / middle && (middle + 1) > x / (middle + 1)) {
            return Math.floor(middle)
        } else if (middle < x / middle) {
            left = middle + 1
        } else { 
            right = middle - 1
        }
    }
}

function sqrt2( x ) {
    // write code here
    return Math.floor(Math.sqrt(x))
}

let now = new Date().getTime();
console.log(sqrt2(10));
console.log(new Date().getTime() - now);